package June;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

// 定义员工类
class Employee {
    private int eid;
    private int age;
    private double salary;
    private String level;

    public int getEid() {
        return eid;
    }

    public void setEid(int eid) {
        this.eid = eid;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public Employee(int eid, int age, double salary, String level) {
        this.eid = eid;
        this.age = age;
        this.salary = salary;
        this.level = level;
    }

    // Getter 和 Setter 略...

    @Override
    public String toString() {
        return "员工{" + "eid=" + eid + ", 年龄=" + age + ", 薪资=" + salary + ", 级别=" + level + '}';
    }
}

// 测试类
public class EmployeeTest {
    public static void main(String[] args) {
        // 创建多个员工对象
        Employee employee1 = new Employee(1, 35, 8000.0, "高级");
        Employee employee2 = new Employee(2, 28, 6000.0, "中级");
        Employee employee3 = new Employee(3, 45, 12000.0, "高级");
        Employee employee4 = new Employee(4, 32, 5000.0, "中级");
        Employee employee5 = new Employee(5, 25, 4500.0, "初级");

        // 创建 ArrayList 集合
        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);
        employeeList.add(employee3);
        employeeList.add(employee4);
        employeeList.add(employee5);

        // 按照薪资降序排列，然后按照年龄升序排列
        Collections.sort(employeeList, new Comparator<Employee>() {
            @Override
            public int compare(Employee e1, Employee e2) {
                int salaryCompare = Double.compare(e2.getSalary(), e1.getSalary()); // 比较薪资，降序排列
                if (salaryCompare != 0) {
                    return salaryCompare; // 如果薪资不同直接返回结果，结束比较
                } else { // 如果薪资相同则比较年龄，升序排列
                    return Integer.compare(e1.getAge(), e2.getAge()); // 使用Integer的compareTo方法比较年龄大小
                }
            }
        }); // 使用自定义的 Comparator 来排序员工列表

        // 遍历集合并输出员工信息
        System.out.println("按照薪资降序，年龄升序排序后的员工列表: "); // 输出排序后的列表信息前先打印提示信息
        for (Employee employee : employeeList) { // 使用增强 for-each 循环遍历并打印每个员工对象的信息
            System.out.println(employee); // 使用对象的 toString() 方法输出对象信息
        }
    }
}